package com.winz.cultureapp.filter.intangible;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.PopupWindow;

import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.IntangibleAdapter;
import com.winz.cultureapp.adapter.SelLocationAdapter;
import com.winz.cultureapp.entity.IntangibleBean;
import com.winz.cultureapp.entity.LocationBean;
import com.winz.filterbar.FilterTab;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目资源库--类型选择
 */
public class IntangiblePopWindow extends PopupWindow implements AdapterView.OnItemClickListener {

    private View window;
    private Context context;
    private int flag ; // 0--项目资源库   1--研究成果及多媒体出版物
    private int position = -1;

    private IntangibleAdapter mAdapter;

    private List<IntangibleBean.DataBean.ListBean> mDatas = new ArrayList<>();

    private OnFinishListener listener;


    public IntangiblePopWindow(Context context, int flag){
        super(context);
        this.context = context;
        this.flag = flag;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        window = inflater.inflate(R.layout.pop_activity_location, null);


        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);

        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //设置SelectPicPopupWindow的View
        this.setContentView(window);
        window.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        initviews(window);
        loadDatas();
    }



    private void initviews(View window) {
        GridView gv_data = window.findViewById(R.id.gv_location);
        window.findViewById(R.id.btn_reset).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAdapter.cleanAllState();
            }
        });
        window.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null){
                    String data = mDatas.get(mAdapter.getLastSeletPosition()).name + "," + mDatas.get(mAdapter.getLastSeletPosition()).id;
                    // 记录选择位置
                    position = mAdapter.getLastSeletPosition();
                    listener.onFinish(flag,position + "-" + data);
                }
                dismiss();
            }
        });

        mAdapter = new IntangibleAdapter(context,true, R.layout.item_sel_location);

        gv_data.setAdapter(mAdapter);
        gv_data.setOnItemClickListener(this);

    }

    private void loadDatas() {
        mDatas.add(new IntangibleBean.DataBean.ListBean("0","全部"));
        mDatas.add(new IntangibleBean.DataBean.ListBean("1","世间文学"));
        mDatas.add(new IntangibleBean.DataBean.ListBean("2","传统技艺"));
        mDatas.add(new IntangibleBean.DataBean.ListBean("3","传统舞蹈"));
        mDatas.add(new IntangibleBean.DataBean.ListBean("4","传统音乐"));
        mDatas.add(new IntangibleBean.DataBean.ListBean("5","传统美术"));
        mDatas.add(new IntangibleBean.DataBean.ListBean("6","传统技艺"));
        mDatas.add(new IntangibleBean.DataBean.ListBean("7","传统戏剧"));
        mDatas.add(new IntangibleBean.DataBean.ListBean("8","传统医药"));
        mDatas.add(new IntangibleBean.DataBean.ListBean("9","曲艺"));
        mDatas.add(new IntangibleBean.DataBean.ListBean("10","名俗"));
        mAdapter.setDatas(mDatas);
    }

    public void dismiss(){
        super.dismiss();
    }

    public void showAsDropDown(View root,int position){
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            root.getGlobalVisibleRect(visibleFrame);
            int height = root.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(root);
        restoreSettings(position);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        mAdapter.changeSel(position);
    }

    public void setOnFinishListener(OnFinishListener listener){
        this.listener = listener;
    }

    public interface OnFinishListener{
        void onFinish(int flag, String data);
    }

    public int getFlag(){
        return flag;
    }

    /**
     * 恢复记录
     */
    public void restoreSettings(int position){
        this.position = position;
        // 每次打开恢复记录
        if(position != -1){
            mAdapter.changeSel(position);
        }else{
            mAdapter.cleanAllState();
        }
    }

}
